package br.unisinos.projetotcc.pesquisa.jarfile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

import org.apache.commons.io.IOUtils;

/**
 * http://stackoverflow.com/questions/8522443/generate-jar-file-during-runtime
 * 
 * @author MARVAN
 *
 */
public class TesteJarClassGenerate {

	public static void main(String[] args) throws IOException {
		
		FileOutputStream fout = new FileOutputStream("C:\\Users\\MARVAN\\Desktop\\jar-folder\\rrr.txt");
		JarOutputStream jarOut = new JarOutputStream(fout);
		jarOut.putNextEntry(new ZipEntry("C:\\Users\\MARVAN\\Desktop\\teste")); // Folders must end with "/".
		jarOut.putNextEntry(new ZipEntry("C:\\Users\\MARVAN\\Desktop\\teste\\Teste.class"));
		jarOut.write(getBytes("C:\\Users\\MARVAN\\Desktop\\teste\\Teste.class"));
		jarOut.closeEntry();
//		jarOut.putNextEntry(new ZipEntry("com/foo/Bar.class"));
//		jarOut.write(getBytes("com/foo/Bar.class"));
		jarOut.closeEntry();
		jarOut.close();
		fout.close();		
	}

	public static byte[] getBytes(String path) throws FileNotFoundException, IOException {
		return IOUtils.toByteArray(new FileInputStream(path));
	}
	
}
